package me.test.neo4j;

import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class Neo4j_TestActivity extends Activity {
    
	private static Retrieval ret;
	private static String out_message;
	private static double time;
	
	public static final String RESULT_MESSAGE = "me.test.neo4j.out_message";
	public static final String RESULT_TIME = "me.test.neo4j.time";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ConfigHelper.BUFFER = false;
    }
    
    public void evaluateWords(View view) {
    	time = 0;
    	out_message = "";
    	EditText pathEdit = (EditText) findViewById(R.id.db_path_edit);
    	String dbPath = pathEdit.getText().toString();
    	if (dbPath.length() > 0) {
    		ConfigHelper.DB_PATH = dbPath;
    		ret = new Retrieval();
        	
        	String[] words = stripWords(getWords());
//        	if (words.length == 0) {
//        		out_message = "Enter words!";
//        	} else {
//        		String letters = getLetters();
//            	
//            	ret.setSentence(words, letters);
//            	time = ret.eval();
//            	
//            	Set<String> results;
//            	results = ret.getSortedKeySet(1);
//            	
//            	if (!results.isEmpty()) {
//            		out_message = "1st word: " + results.toArray()[0];
//            	} else {
//            		out_message = "No results found!";
//            	}
//        	}
    	} else {
    		out_message = "Enter database path!";
    	}
    	
    	Intent showResults = new Intent(this, Neo4j_ResultsActivity.class);
    	showResults.putExtra(RESULT_MESSAGE, out_message);
    	showResults.putExtra(RESULT_TIME, "" + time);
    	startActivity(showResults);
    }
    
    public String[] getWords() {
    	String[] words = new String[4];
    	
    	EditText w1Edit = (EditText) findViewById(R.id.word1_edit);
    	String word1 = w1Edit.getText().toString();
    	words[0] = word1;
    	
    	EditText w2Edit = (EditText) findViewById(R.id.word1_edit);
    	String word2 = w2Edit.getText().toString();
    	words[1] = word2;
    	
    	EditText w3Edit = (EditText) findViewById(R.id.word1_edit);
    	String word3 = w3Edit.getText().toString();
    	words[2] = word3;
    	
    	EditText w4Edit = (EditText) findViewById(R.id.word1_edit);
    	String word4 = w4Edit.getText().toString();
    	words[3] = word4;
    	
    	return words;
    }
    
    public String[] stripWords(String[] rawWords) {
    	int length = 0;
    	for (int i = 0; i < rawWords.length; i ++) {
    		if (rawWords[i].length() > 0 && rawWords[i] != null) {
    			length ++;
    		}
    	}
    	String[] strippedWords = new String[length];
    	length = 0;
    	for (int i = 0; i < rawWords.length; i ++) {
    		if (rawWords[i].length() > 0 && rawWords[i] != null) {
    			strippedWords[length] = rawWords[i];
    			length ++;
    		}
    	}
    	return strippedWords;
    }
    
    public String getLetters() {
    	String letters = "";
    	
    	EditText lettersEdit = (EditText) findViewById(R.id.letters_edit);
    	letters = lettersEdit.getText().toString();
    	
    	return letters;
    }
}